﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Cms;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [JwtAuthorize(Roles = "Admin")]
    [ApiController]
    public class ColumnController : ControllerBase
    {
        private readonly ICmsColumnService _classService;
        public ColumnController(ICmsColumnService classService)
        {
            _classService = classService;
        }

        /// <summary>
        /// 根据栏目列表，提供下拉框使用
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("select")]
        public async Task<IActionResult> GetSelectList([FromBody]ParmInt obj)
        {
            var siteGuid = SiteTool.CurrentSite?.Guid;
            var dblist = await _classService.GetListAsync(m => m.SiteGuid == siteGuid && m.TypeID == 2, m => m.Sort, DbOrderEnum.Asc);
            var list = _classService.RecursiveModule(dblist.data);
            foreach (var item in list)
            {
                item.Title = Utils.LevelName(item.Title, item.ClassLayer);
            }
            return Ok(new ApiResult<List<CmsColumn>>()
            {
                data = list,
                statusCode = dblist.statusCode,
                message = dblist.message
            });
        }

        /// <summary>
        /// 根据id获得分类信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmInt obj)
        {
            var siteGuid = SiteTool.CurrentSite?.Guid;
            var dblist = await _classService.GetListAsync(m => m.SiteGuid == siteGuid && m.TypeID==2, m => m.Sort, DbOrderEnum.Asc);
            var list = _classService.RecursiveModule(dblist.data);
            foreach (var item in list)
            {
                item.Title = Utils.LevelName(item.Title, item.ClassLayer);
            }
            var columnModel = await _classService.GetModelAsync(m => m.Id == obj.id);
            var resDto = new GetColumnDto()
            {
                model= columnModel.data,
                list=list
            };
            return Ok(new ApiResult<GetColumnDto>()
            {
                data = resDto,
                statusCode = columnModel.statusCode,
                message = columnModel.message
            });
        }

        /// <summary>
        /// 添加分类信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]CmsColumn model)
        {
            model.TempId = 19;
            model.TypeID = 2;
            model.SiteGuid = SiteTool.CurrentSite?.Guid;
            return Ok(await _classService.AddAsync(model));
        }

        /// <summary>
        /// 修改分类信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]CmsColumn model)
        {
            return Ok(await _classService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmInt obj)
        {
            return Ok(await _classService.DeleteAsync(m=>m.Id==obj.id || m.ClassList.Contains(obj.id.ToString())));
        }

        /// <summary>
        /// 移动端是否显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("show")]
        public async Task<IActionResult> ShowOrHide([FromBody]ParmInt obj)
        {
            var model =await _classService.GetModelAsync(m=>m.Id==obj.id);
            if (model.data.IsWapShow)
            {
                model.data.IsWapShow = false;
            }
            else {
                model.data.IsWapShow = true;
            }
            return Ok(await _classService.UpdateAsync(model.data));
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("tree")]
        public async Task<IActionResult> GetTree([FromBody]ParmInt obj)
        {
            var siteGuid = SiteTool.CurrentSite?.Guid;
            return Ok(await _classService.TreeAsync(obj.id, siteGuid));
        }

        /// <summary>
        /// 自定义排序
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("sort")]
        public async Task<IActionResult> CurStor([FromBody]List<ColumnTree> list)
        {
            var siteGuid = SiteTool.CurrentSite?.Guid;
            return Ok(await _classService.CurSort(list,2,siteGuid));
        }
    }
}